package emby.lang;

import emby.err.InvalidArgumentError;
import emby.err.TypeError;

public class NativeCastingFunctions {

	public final static NativeFunction Int = new NativeFunction("int", 1){
		private static final long serialVersionUID = 3481959836544692443L;

		public Object execute(int line, Scope s, Object... args){
			if( args==null || args.length!=1){
				throw new InvalidArgumentError(line,"int requires 1 argument");
			}
			if( args[0] instanceof Number){
				return ((Number)args[0]).intValue();
			}else if (args[0] instanceof String){
				return ((Number)Integer.parseInt((String)args[0])).intValue();
			}else{
				throw new TypeError(line,args[0]+" is not castable to int");
			}

		}
	};

	public final static NativeFunction Dbl = new NativeFunction("double", 1){
		private static final long serialVersionUID = -7311179603413825672L;

		public Object execute(int line, Scope s, Object... args){
			if( args==null || args.length!=1){
				throw new InvalidArgumentError(line,"double requires 1 argument");
			}
			if( args[0] instanceof Number){
				return ((Number)args[0]).doubleValue();
			}else if (args[0] instanceof String){
				return Double.parseDouble((String)args[0]);
			}else{
				throw new TypeError(line,args[0]+" is not castable to double");
			}

		}
	};
}
